<template>
	<view class="detail">
		<view class="" style="min-height: 300rpx;background-color: #333;">
			<Info v-if="info" :info="info" />
		</view>
		
		<view class="" style="background-color: #333;">
			<view class="title-bar">
				<view class="">
					简介
				</view>
				<view class="" v-if="chapters.length > 0">
					章节目录
				</view>
				<view class="" v-if="pages">
					图片预览
				</view>
			</view>
		</view>
		
		<view class="block">
			<view class="title">
				简介
			</view>
			<view class="eontent desc" v-if="info">
				{{ info.desc }}
			</view>
		</view>
		
		<view class="block" v-if="chapters.length > 0">
			<view class="title">
				章节目录
			</view>
			<view class="eontent chaptr">
				<view class="" v-for="chaptr in chapters" :key="chaptr.id">
					{{ chaptr.title }}
				</view>
			</view>
		</view>
		
		<view class="block" v-if="images.length > 0">
			<view class="title">
				图片预览
			</view>
			<view class="eontent chaptr">
				<!-- <custom-waterfalls-flow :value="images" @imageClick="onClick" :column="3"></custom-waterfalls-flow> -->
				<!-- <Pictures :value="images" :column="3"></Pictures> -->
				<ImageVirtualList :list="images" :column="3">
					<template v-slot:default="{item, index}">
					    <image :src="item.image" mode="aspectFill" style="width: 100%; height: 100%;" @click="onClick(index)"></image>
						<uni-icons type="vip-filled" size="30" color="#f00" style="position: absolute; margin-left: -30px; margin-top: -10px;"></uni-icons>
					</template>
				</ImageVirtualList>
			</view>
		</view>
		
		<view class="nav" :style="{'padding-bottom':buttom}">
			<view class="" style="display: flex; align-items: center; font-size: 24rpx;">
				<image src="../../../static/书架.svg" mode="aspectFill" class="icon" style="width: 30rpx; height: 30rpx; margin-right: 20rpx;"></image>
				加入藏经架
			</view>
			<view class="">
				<button @click="toContent('')" style="background-color: #f00; border-radius: 28rpx; color: #fff; padding: 0 60rpx;" size="mini">开始阅读</button>
			</view>
		</view>
		
		<view class="" :style="{'padding-bottom':buttom}">
			<view class="" style="height: 100rpx;" />
		</view>
	</view>
	
</template>

<script>
	import Info from "./components/info.vue"
	import ImageVirtualList from "@/components/virtual-list.vue"
	import http from "@/utils/http.js"
	export default{
		data(){
			return {
				info: null,
				chapters: [],
				pages: null,
				buttom: uni.getWindowInfo().safeAreaInsets.bottom + 'px',
				images: [],
			}
		},
		components: {Info, ImageVirtualList},
		onLoad(p) {
			const _this = this
			http.get('/detail/'+p.id).then(r => {
				if(r.data.type == 'txt') {
					_this.chapters = r.data.chapters
					delete r.data.chapters
				}
				else {
					_this.pages = r.data.pages
					_this.images = _this.pages.pages.map(item => {return {image: item.content}})
					console.log(_this.images)
					delete r.data.pages
				}
				_this.info = r.data;
			})
		},
		methods: {
			toContent(chapter_id){
				let url, opend;
				switch(this.info.type) {
					case 'txt':
					url = "/pages/document/read/txt?id=" + this.info.id + "&chapter_id=" + chapter_id + "&page_count=" + this.info.page_count;
					opend = () => {}
					break;
					
					case 'pdf':
					url = "/pages/document/read/image?index=" + chapter_id
					opend = (res) => {
						res.eventChannel.emit('init', this.images)
					}
				}
				uni.navigateTo({
					url,
					success: opend
				})
			},
			onClick(index){
				this.toContent(index)
			}
		}
	}
</script>

<style scoped>
	.title-bar{
		background-color: #fff;
		height: 70rpx;
		border-top-left-radius: 50rpx;
		border-top-right-radius: 50rpx;
		display: flex;
		justify-content: space-around;
		align-items: flex-end;
		font-size: 30rpx;
	}
	.block{
		padding: 30rpx;
	}
	.eontent{
		font-size: 24rpx;
		line-height: 2;
		
		color: #999;
	}
	.desc{
		text-indent: 48rpx;
	}
	.title{
		font-size: 28rpx;
		font-weight: bold;
		margin-bottom: 20rpx;
	}
	.nav{
		height: 100rpx;
		background-color: #fff;
		border-top: 1rpx solid #999;
		position: fixed;
		bottom: 0;
		width: 690rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 10rpx 30rpx;
	}
</style>